package com.fuxin.hanfu.service;

import com.fuxin.hanfu.domain.User;
import com.fuxin.hanfu.mapper.UserMapper;
import com.fuxin.hanfu.uitls.MyResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public List<User> findList() {
        return userMapper.selectByExample(null);
    }

    public User findById(Integer userId) {
        return userMapper.findById(userId);
    }

    public Integer insert(User user) {
        try {
            return userMapper.insert(user);
        } catch (Exception e) {
            // 记录日志
            System.err.println("Error inserting user: " + e.getMessage());
            throw e; // 重新抛出异常以便上层处理
        }

    }

    public Integer update(User user) {
        return userMapper.update(user);
    }

    public Integer delete(Integer userId) {
        return userMapper.delete(userId);
    }


    public Integer insertUser(User user) {
        return userMapper.insertUser(user);

    }

    public User findByEmail(String email) {

        return null;
    }


    public User findByUsernameAndPassword(String username, String password) {
        // 这里应添加密码加密验证的逻辑，比如使用BCrypt
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) { // 注意：这里不应直接比较明文密码
            return user;
        }
        return null;
    }
}
